﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class RateDesigner : CellTypeDesigner<Rate>
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new RateDrawingObject(this.CellType, drawingHelper);
        }
    }

    [Designer("Vant.CellTypes.RateDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Rate.png")]
    [OrderWeight((int)Utils.OrderWeight.Rate)]
    public class Rate : VantCellTypeBase, ICommandCellType, ISupportReadOnly, ISupportDisable, ISupportDefaultValue, ISupportUIPermission
    {
        [SRDisplayName(nameof(Resources.Vant_EditCommands))]
        public List<Command> CommandList { get; set; } = new List<Command>();

        public CommandExcuteKind CommandExcuteKind => CommandExcuteKind.OnValueChanged;

        List<UIPermission> _uIPermissions;
        [SRDisplayName(nameof(Resources.Vant_UIPermissions))]
        [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
        public List<UIPermission> UIPermissions
        {
            get
            {
                if (_uIPermissions == null)
                {
                    _uIPermissions = Utils.GetDefaultPermission(UIPermissionScope.Enable, UIPermissionScope.Visible, UIPermissionScope.Editable);
                }
                return _uIPermissions;
            }
            set
            {
                _uIPermissions = value;
            }
        }

        [SRDisplayName(nameof(Resources.Rate_Default))]
        [FormulaProperty]
        public object DefaultValue { get; set; }

        [SRDisplayName(nameof(Resources.Rate_IconCount))]
        [DefaultValue(5)]
        public int iconCount { get; set; } = 5;

        [SRDisplayName(nameof(Resources.Rate_IconSize))]
        [IntProperty(Min = 12)]
        [DefaultValue(20)]
        public int iconSize { get; set; } = 20;

        [SRDisplayName(nameof(Resources.Rate_IconGutter))]
        [IntProperty(Min = 4)]
        [DefaultValue(4)]
        public int iconGutter { get; set; } = 4;

        [SRDisplayName(nameof(Resources.Rate_Color))]
        [ColorProperty]
        public string color { get; set; } = "#ee0a24";

        [SRDisplayName(nameof(Resources.Rate_VoidColor))]
        [ColorProperty]
        public string voidColor { get; set; } = "#c8c9cc";

        [SRDisplayName(nameof(Resources.Rate_IsDisabled))]
        [BoolProperty]
        public bool IsDisabled { get; set; } = false;

        [SRDisplayName(nameof(Resources.Rate_DisabledColor))]
        [ColorProperty]
        public string disabledColor { get; set; } = "#c8c9cc";

        [SRCategoryHeader(nameof(Resources.Other))]
        [SRDisplayName(nameof(Resources.Rate_AllowHalf))]
        public bool allowHalf { get; set; } = false;

        [SRDisplayName(nameof(Resources.Rate_Touchable))]
        [DefaultValue(true)]
        public bool touchable { get; set; } = true;

        [SRDisplayName(nameof(Resources.Vant_Readonly))]
        public bool ReadOnly { get; set; } = false;

        public override SupportFeatures SupportFeatures
        {
            get
            {
                return SupportFeatures.ShouldCheckDirtyWhenLeavePage;
            }
        }

        [Browsable(false)]
        [JsonIgnore]
        public bool NeedFormatDefaultValue { get; set; } = true;

        public override string ToString()
        {
            return Properties.Resources.Rate;
        }
    }
}
